#import <Foundation/Foundation.h>
#import "SFUEventCollection.h"
#import "SFUCalendarSource.h"
#import "SFUConstants.h"
#import "SFUEventSearchEngineDelegate.h"
#import "WebCacheOperation.h"

/**
 * An SFUEventSearchEngine is an object modelling a search engine
 * for all relevant events at SFU.
 *
 * Contains the static list of parsable websites with updated
 * information on events at SFU. The SFUEventSearchEngine retains
 * a cached collection of these SFU events for matching with 
 * various search parameters.
 *
 * Results of the event search engine are handled by the event search
 * engine delegate. Thus the delegate must be set prior to any search
 * queries performed.
 *
 * Conforms to the SFUConnectionDelegate protocol.
 *
 * @author      James Lin
 * @version     1.2.0
 * @since       0.4.0
 * @see         SFUConnectionDelegate
 */
@interface SFUEventSearchEngine : NSObject <SFUConnectionDelegate> {
	id<SFUEventSearchEngineDelegate> delegate;           // Delegate handling search events
	NSUInteger                       webSourceCount;     // Website connection count
	NSArray*                         searchKeywords;     // Keywords of the latest search query
	CGFloat	                         expectedSize;       // Estimated size of HTTP load requests
	CGFloat                          currentSize;        // Current size of HTTP load requests 
	UIActivityIndicatorView*         progressIndicator;  // Search progress indicator
	UIAlertView*                     progressView;       // Search progress popup view
	BOOL                             matchAllKeywords;   // Whether to match all search keywords
	BOOL                             locationEventsOnly; // Whether to take only events with location
	BOOL                             silentSearch;       // Whether the search is visual
	NSNumber*                        cacheCount;         // Caching operation count
	BOOL                             searchRequested;    // Whether a search was requested
}

@property (nonatomic, retain) NSArray* searchKeywords;

/**
 * Retrieve the shared instance of the SFU event search engine.
 *
 * The event cache is empty upon initialization of this shared instance.
 *
 * @return  the shared instance of SFUEventSearchEngine
 */
+ (SFUEventSearchEngine*)sharedSearchEngine;

/**
 * Get the delegate handling search events.
 *
 * @return  the delegate handling search events
 * @see     SFUEventSearchEngineDelegate
 */
- (id<SFUEventSearchEngineDelegate>)delegate;

/**
 * Set the delegate handling search events.
 *
 * @param theDelegate  the delegate handling search events
 * @see                SFUEventSearchEngineDelegate
 */
- (void)setDelegate:(id<SFUEventSearchEngineDelegate>)theDelegate;

/**
 * Search for all SFU events with their headings or categories 
 * containing any or all of the keywords in the given list of 
 * search keywords.
 *
 * Searching by any keyword in the list will return events sorted by
 * non-increasing order of number of keywords matched.
 *
 * An empty keyword list is valid and will search for all SFU events.
 * Enabling use of the cached events will result in an attempt to
 * search the the precached events. If the cache is empty, a full 
 * parse from website sources will be done prior to search.
 *
 * The results of the search will be passed to the delegate handling
 * search events. Thus this delegate must be set prior to the search 
 * query.
 *
 * @param keywords  the list of search keywords
 * @param flag      whether to use the event cache for the search
 * @param matchAll  YES if all keywords in the keyword list must match;
 *                  NO if any keyword can match.
 */
- (void)findEventsWithKeywords:(NSArray*)keywords 
					  useCache:(BOOL)flag 
					  matchAll:(BOOL)matchAll;

/**
 * Search for all SFU events with their headings or categories 
 * containing any or all of the keywords in the given list of 
 * search keywords.
 *
 * An empty keyword list is valid and will search for all SFU events.
 * Does not use cached events by default.
 *
 * The results of the search will be passed to the delegate handling
 * search events. Thus this delegate must be set prior to the search 
 * query.
 *
 * @param keywords  the list of search keywords
 * @param matchAll  YES if all keywords in the keyword list must match;
 *                  NO if any keyword can match.
 */
- (void)findEventsWithKeywords:(NSArray*)keywords 
			  matchAllKeywords:(BOOL)matchAll;

/**
 * Search for all SFU events with their headings or categories containing 
 * any or all of the keywords in the given space-delimited keyword string.
 *
 * @precondition    keyword string is not nil
 * @param keyword   the case-insensitive search keyword string
 * @param matchAll  YES if all keywords in the keyword list must match;
 *                  NO if any keyword can match.
 */
- (void)findEventsWithKeywordString:(NSString*)keyword
				   matchAllKeywords:(BOOL)matchAll;

/**
 * Execute an update of the event cache.
 */
- (void)cacheEvents;

@end
